class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() == 0)  return 0;
        /*
        (2020/12/16自)直接双指针计数:first指向没有重复字符的最长字符串开头位置，last指向尾部，
        当s[last]为重复字符串时，first转而指向串中与last重复的字符串后一个位置，反之++last。
        */
        int max = 0;
        int count = 0;
        int first = 0;
        int last = 0;
        int sizeOfS = s.size();
        while(last < sizeOfS)
        {
            int first_copy = first;
            bool hasTheSame = false;
            while(first_copy < last)
            {
                if(s[first_copy] == s[last])
                {
                    first = ++first_copy;
                    hasTheSame = true;
                    break;
                }
                ++first_copy;
            }

            if(hasTheSame)  count = last - first + 1;
            else  ++count;
            if(count > max)  max = count;
            ++last;
        }

        return max;
    }
};